package com.whu.helpluojia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.helpluojia.common.BaseContext;
import com.whu.helpluojia.dao.BagmanDao;
import com.whu.helpluojia.domain.Bagman;
import com.whu.helpluojia.domain.MyPublish;
import com.whu.helpluojia.domain.Supply;
import com.whu.helpluojia.domain.User;
import com.whu.helpluojia.service.BagManService;
import com.whu.helpluojia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.whu.helpluojia.util.DistanceUil.getDistance;

@Service
public class BagManServiceImpl extends ServiceImpl<BagmanDao, Bagman> implements BagManService {

    @Autowired
    BagmanDao bagManDao;

    @Autowired
    UserService userService;

    @Override
    public List<Bagman> getByDate(Date date) {
        LambdaQueryWrapper<Bagman> lqw = new LambdaQueryWrapper<Bagman>();
        lqw.eq(Bagman::getTime,date);
        return bagManDao.selectList(lqw);
    }
    @Override
    public Bagman[] selectAll() {
//        QueryWrapper<BagMan> bagManQueryWrapper = new QueryWrapper<>();
//        bagManQueryWrapper.isNotNull("id");
        //return bagManDao.selectObjs(bagManQueryWrapper).toArray(new BagMan[0]);
        List<Bagman> list = bagManDao.selectList(null);
        Bagman[] array = new Bagman[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }

    @Override
    public List<Bagman> getByUser(Long userId) {
        LambdaQueryWrapper<Bagman> lqw = new LambdaQueryWrapper<Bagman>();
        lqw.eq(Bagman::getUserId,userId);
        return bagManDao.selectList(lqw);
    }

    @Override
    public List<Bagman> getByType(String type) {
        LambdaQueryWrapper<Bagman> lqw = new LambdaQueryWrapper<Bagman>();
        lqw.eq(Bagman::getType,type);
        return bagManDao.selectList(lqw);
    }

    @Override
    public List<Bagman> getByDescription(String description) {
        LambdaQueryWrapper<Bagman> lqw = new LambdaQueryWrapper<Bagman>();
        lqw.like(Bagman::getDescription,description);
        return bagManDao.selectList(lqw);
    }

    @Override
    public List<Bagman> getByDestination(double Lat, double Lon,int length) {
        List<Bagman> list = bagManDao.selectList(null);
        for(Bagman bagman:list){
            if(getDistance(bagman.getStartLon(),bagman.getStartLat(),Lon,Lat)>length){
                list.remove(bagman);
            }
        }
        LambdaQueryWrapper<Bagman> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(1.00 != Lon,Bagman::getId,Lon);
        return list;
    }

    @Override
    public void addBagman(Bagman bagman) {

        Long userId=BaseContext.getCurrentId();

        bagman.setTime(new Date());

        bagman.setUserId(userId);

        bagman.setStatus(0);

        System.out.println("bagman:"+bagman);

        bagManDao.insert(bagman);
    }

    @Override
    public Bagman queryBagmanById(Long id) {
        return bagManDao.selectById(id);
    }


    @Override
    public List<MyPublish> getMyDetail(Long userId) {

        LambdaQueryWrapper<Bagman> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Bagman::getUserId,userId);
        List<Bagman>bagmanList=bagManDao.selectList(lambdaQueryWrapper);

        User user=userService.selectUser(userId);

        List<MyPublish> myPublishes=new ArrayList<>();

        for(Bagman bagman:bagmanList){
            MyPublish myPublish=new MyPublish();
            myPublish=copyData(myPublish,bagman,user);
            myPublishes.add(myPublish);
        }

        return myPublishes;
    }

    //获取我的界面数据
    @Override
    public MyPublish getBagmanDetail(Long id,Long userId) {

        Bagman bagman=bagManDao.selectById(id);

        User user=userService.selectUser(userId);

        MyPublish myPublish=new MyPublish();

        myPublish=copyData(myPublish,bagman,user);

        return myPublish;
    }

    @Override
    public int deleteBagman(Long id) {
        return bagManDao.deleteById(id);
    }

    @Override
    public void solute(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", 1);
        bagManDao.update(null, updateWrapper);
    }

    public MyPublish copyData(MyPublish myPublish, Bagman bagman, User user){
        myPublish.setId(bagman.getId());
        myPublish.setCategory("求跑腿");
        myPublish.setContent(bagman.getDescription());
        myPublish.setSex(user.getGender());
        myPublish.setStatus(bagman.getStatus());
        myPublish.setSenderName(user.getName());
        myPublish.setTime(bagman.getTime());
        myPublish.setType(bagman.getType());
        return myPublish;
    }
}


